﻿using Microsoft.AspNetCore.Mvc.Rendering;
using System.Text.Encodings.Web;

namespace UI
{
    public static class PrepageUrlHelper
    {
        public const string PREPAGE = "prepage";

        /// <summary>
        /// 从当前url中得到应该返回的之前页面路径
        /// </summary>
        /// <returns>带prepage=的字符串</returns>
        public static string GetPrepage(this HttpRequest request)
        {
            //note: 必须每次从HttpContext.Current里面取，不要用类的static成员
            string prepage = request.Query[PREPAGE];

            if (string.IsNullOrEmpty(prepage))
            {
                prepage = request.Path + request.QueryString;
            }//else nothing

            return $"{PREPAGE}={UrlEncoder.Default.Encode(prepage)}";
        }

        ///// <summary>
        ///// 返回之前页面
        ///// </summary>
        ///// <param name="defaultUrl">如果Request中取不到之前页面，默认返回的url地址</param>
        ///// <returns></returns>
        //public static RedirectResult Return(string defaultUrl)
        //{
        //    var request = HttpContext.Current.Request;
        //    string prepage = HttpContext.Current.Request.QueryString[PREPAGE];

        //    if (string.IsNullOrEmpty(prepage))
        //    {
        //        return new RedirectResult(defaultUrl);
        //    }
        //    else
        //    {
        //        return new RedirectResult(prepage);
        //    }
        //}

        //public static RedirectResult Return(RunOn run = RunOn.PC)
        //{
        //    switch (run)
        //    {
        //        case RunOn.PC:
        //            return Return(@"/Code");
        //        case RunOn.Wechat:
        //            return Return(@"/Code/Home/VIndex");
        //        default:
        //            throw new Exception($"未处理的enum：{run}");
        //    }
        //}
    }


}
